﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NAccounting.QueryObjects
{
    public class AccountEntryQueryObject : QueryObject<AccountEntry>
    {
        public int? LotId { get; set; }
        public TimePeriod? Period { get; set; }

        public AccountEntryQueryObject(NAccountEntities context)
            : base(context)
        { }

        public override PagedResult<AccountEntry> Get(int pageIndex, int rows)
        {
            var result = new PagedResult<AccountEntry>();
            result.Total = Context.AccountEntries
                                   .Where(Condition)
                                   .Count();

            result.List = Context.AccountEntries
                                 .Where(Condition)
                                 .OrderBy(x => x.Description)
                                 .Skip(pageIndex * rows)
                                 .Take(rows).ToArray();
            return result;
        }

        protected override bool Condition(AccountEntry entity)
        {
            return (!LotId.HasValue || entity.LotId == LotId.Value) &&
                   (!Period.HasValue || entity.Date >= Period.Value.Start && entity.Date <= Period.Value.End);
        }
    }
}